package com.photo.tagger;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class TagUI extends Activity 
{
	private Bitmap myBitmap;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.second);
        
        Log.i("tag", "scond lo first line");
        BitmapFactory.Options bitmapFatoryOptions=new BitmapFactory.Options();
		bitmapFatoryOptions.inPreferredConfig=Bitmap.Config.RGB_565;
		Log.i("tag", "options aipoinai");
		myBitmap= BitmapFactory.decodeByteArray(
                getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length,bitmapFatoryOptions);
		Log.i("tag", "bitmap");
		//ImageView preview= (ImageView) findViewById(R.id.resultImage);
		//preview.setImageBitmap(myBitmap);
		//Bitmap c=myBitmap.createScaledBitmap(myBitmap, 200, 200, true);
		Log.i("tag", "myview set kale inka");
        setContentView(new MyView(this));
    }
    
    private class MyView extends View
    {
    	public Bitmap bitmap;
    	private int width, height;
    	private FaceDetector.Face[] detectedFaces;
    	private int NUMBER_OF_FACES=4;
    	private FaceDetector faceDetector;
    	private int NUMBER_OF_FACE_DETECTED;
    	private float eyeDistance;
    	
		public MyView(Context context) 
		{
			super(context);
			bitmap=TagUI.this.myBitmap;
			//Log.i("tag", "myview set kale inka");
			//int s=bitmap.describeContents();
			//Log.i("tag", "height, width set ainai");
			width=TagUI.this.myBitmap.getWidth();
			height=TagUI.this.myBitmap.getHeight();
			Log.i("tag", "height, width set ainai");
			detectedFaces=new FaceDetector.Face[NUMBER_OF_FACES];
			faceDetector=new FaceDetector(width,height,NUMBER_OF_FACES);
			Log.i("tag", "face detection start aindhi");
			NUMBER_OF_FACE_DETECTED=faceDetector.findFaces(bitmap, detectedFaces);
			Log.i("tag", "faces vachesnai ro");
		}
		
		@Override
		protected void onDraw(Canvas canvas)
		{
			canvas.drawBitmap(bitmap,0,0, null);
			Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE); 	
            myPaint.setStrokeWidth(3);
            

            for(int count=0;count<NUMBER_OF_FACE_DETECTED;count++)
            {
            	Face face=detectedFaces[count];
            	PointF midPoint=new PointF();
            	face.getMidPoint(midPoint);
            	
            	eyeDistance=face.eyesDistance();
            	canvas.drawRect(midPoint.x-eyeDistance, midPoint.y-eyeDistance, midPoint.x+eyeDistance, midPoint.y+eyeDistance, myPaint);
            }
		}
    	
    }
}